// my_other/dizhi/dizhi.js
let app = getApp()
let yuming = app.globalData.yuming;
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({

  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    title: '地址',
    page: 1,
    list: [],
    gongwu: false,
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.list
    this.setData({
      page: (this.data.page) + 1
    })
    this.getAddressList();
  },
  getAddressList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this;
    this.myRequest(yuming + 'Hallfood/userAddressList', {
        shopId: wx.getStorageSync('shopId'),

      page: that.data.page,
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        }
        this.setData({
          list: shuzong.concat(shu), //地址列表
        })
        let value = this.data.list.filter(item => item.isDefault == 1)[0].id;
        wx.setStorageSync('addressid', value);
      }
    })
  },
  delete(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '否',
      confirmText: '是',
      success(res) {
        if (res.confirm) {
          console.log("是")
          that.shanchu(id)
        } else {
          console.log("否")
        }
      }
    })
  },
  shanchu(id) {
    this.myRequest(yuming + 'Hallfood/userAddressDel', {
      id: id,
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        this.setData({
          page: 1,
          gongwu: false
        })
        shuzong = []
        this.getAddressList();
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  bianji(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/my_other/bianji_dizhi/bianji_dizhi?type=2&id=' + id,
    })
  },
  tianjia() {
    wx.navigateTo({
      url: '/my_other/bianji_dizhi/bianji_dizhi?type=1',
    })
  },
  radioChange(e) {
    wx.setStorageSync('addressid', e.detail.value)
  },
  queren(e) {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddressList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})